//
//  PostsDetailVoiceView.m
//  TestApplication
//
//  Created by Hanlingyu on 16/9/23.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import "PostsDetailVoiceView.h"
#import "UIButton+WebCache.h"
#import "TAMacro.h"
#import "DetailLikedHeaderCollectionViewCell.h"
#import "HomeChannelDetailMoreCollectionViewCell.h"
#import "DetailCommentWithOtherTableViewCell.h"
#import "NoCommentTableViewCell.h"

#import "PostsDetailViewModel.h"
#import "HomeChannelDetailModel.h"
#import "HomeChannelDetailContent.h"
#import "HomeChannelDetailImg.h"
#import "HomeChannelDetailTag.h"
#import "HomeChannelDetailLikeusers.h"
#import "HomeChanneldetailCommentModel.h"
#import "UIImageView+WebCache.h"
#import "SelfSizeTool.h"
#import "NSString+Time.h"
#import "ChannelVoice.h"

#import "VoicePlayerViewController.h"

@interface PostsDetailVoiceView ()
<
    UICollectionViewDelegate,
    UICollectionViewDataSource,
    UITableViewDelegate,
    UITableViewDataSource
>

@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UIButton *headerImageButton;
@property (nonatomic, weak) UIImageView *vipImageView;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UIImageView *voiceImage;
@property (nonatomic, weak) UIButton *playButton;
@property (nonatomic, weak) UILabel *totalTime;
@property (nonatomic, weak) UILabel *descLabel;
@property (nonatomic, weak) UILabel *tagsLabel;

@property (nonatomic, weak) UICollectionView *likePersonCollectionView;
@property (nonatomic, weak) UIImageView *heaterImage;
@property (nonatomic, weak) UIImageView *messegeImageView;
@property (nonatomic, weak) UITableView *commentTableView;
@property (nonatomic, weak) UIView *lineView;
@property (nonatomic, weak) UIView *secLineView;

@end


@implementation PostsDetailVoiceView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createView];
        
    }
    return self;
}


- (void)createView {
    // 头像
    UIButton *headerImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    //    headerImageButton.backgroundColor = [UIColor redColor];
    [self addSubview:headerImageButton];
//    [headerImageButton addTarget:self action:@selector(clickHeaderImage:) forControlEvents:UIControlEventTouchUpInside];
    _headerImageButton = headerImageButton;
    
    // vip
    UIImageView *vipImageView = [[UIImageView alloc] init];
    //    vipImageView.backgroundColor = [UIColor cyanColor];
    vipImageView.image = [UIImage imageNamed:@"vipAvatar"];
    [self addSubview:vipImageView];
    _vipImageView = vipImageView;
    
    
    // 昵称
    UILabel *nameLabel = [[UILabel alloc] init];
    //    nameLabel.backgroundColor = [UIColor yellowColor];
    nameLabel.font = [UIFont systemFontOfSize:15];
    [self addSubview:nameLabel];
    _nameLabel = nameLabel;
    
    // 时间
    UILabel *timeLabel = [[UILabel alloc] init];
    //    timeLabel.backgroundColor = [UIColor greenColor];
    [self addSubview:timeLabel];
    _timeLabel = timeLabel;
    

    // 图片背景
    UIImageView *voiceImage = [[UIImageView alloc] init];
    [self addSubview:voiceImage];
    _voiceImage = voiceImage;
    
    // 播放按钮
    UIButton *playButton = [UIButton buttonWithType:UIButtonTypeCustom];
//    playButton.backgroundColor = [UIColor yellowColor];
    [self addSubview:playButton];
    [playButton setImage:[UIImage imageNamed:@"MusicPlayer_暂停"] forState:UIControlStateNormal];
    [playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    _playButton = playButton;
    
    
    // 播放时长
    UILabel *totalTime = [[UILabel alloc] init];
//    totalTime.backgroundColor = [UIColor blackColor];
    totalTime.textColor = [UIColor whiteColor];
    totalTime.font = [UIFont systemFontOfSize:30 weight:5];
    totalTime.textAlignment = 1;
    [self addSubview:totalTime];
    _totalTime = totalTime;
    
    // 详细
    UILabel *descLabel = [[UILabel alloc] init];
    //    descLabel.backgroundColor = [UIColor lightGrayColor];
    descLabel.numberOfLines = 0;
    descLabel.textColor = [UIColor grayColor];
    descLabel.font = [UIFont systemFontOfSize:14];
    [self addSubview:descLabel];
    _descLabel = descLabel;
    
    // 标签
    UILabel *tagsLabel = [[UILabel alloc] init];
    //    tagsLabel.backgroundColor = [UIColor yellowColor];
    tagsLabel.numberOfLines = 0;
    tagsLabel.textColor = [UIColor lightGrayColor];
    tagsLabel.font = [UIFont systemFontOfSize:12];
    [self addSubview:tagsLabel];
    _tagsLabel = tagsLabel;
    
    // 点赞标志
    UIImageView *heartImage= [[UIImageView alloc] init];
    //    heartImage.backgroundColor = [UIColor redColor];
    heartImage.image = [UIImage imageNamed:@"heart"];
    [self addSubview:heartImage];
    _heaterImage = heartImage;
    
    
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
    [self addSubview:lineView];
    _lineView = lineView;
    
    
    // 点赞人数
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((SCREEN_WIDTH - 60) / 8, (SCREEN_WIDTH - 60) / 8);
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    
    UICollectionView *likePersonCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
    likePersonCollectionView.backgroundColor = [UIColor whiteColor];
    likePersonCollectionView.dataSource = self;
    likePersonCollectionView.delegate = self;
    [self addSubview:likePersonCollectionView];
    [likePersonCollectionView registerNib:[UINib nibWithNibName:@"DetailLikedHeaderCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"DetailLikedHeaderCollectionViewCellReuse"];
    [likePersonCollectionView registerNib:[UINib nibWithNibName:@"HomeChannelDetailMoreCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"HomeChannelDetailMoreCollectionViewCellReuse"];
    _likePersonCollectionView = likePersonCollectionView;
    
    UIView *secLineView = [[UIView alloc] init];
    secLineView.backgroundColor = [UIColor colorWithWhite:0.917 alpha:1];
    [self addSubview:secLineView];
    _secLineView = secLineView;
    
    
    // 评论图片
    UIImageView *messegeImageView = [[UIImageView alloc] init];
    //    messegeImageView.backgroundColor = [UIColor redColor];
    messegeImageView.image = [UIImage imageNamed:@"add_comment_normal"];
    [self addSubview:messegeImageView];
    _messegeImageView = messegeImageView;
    
    // 评论
    UITableView *commentTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    commentTableView.backgroundColor = [UIColor yellowColor];
    commentTableView.dataSource = self;
    commentTableView.delegate = self;
    [self addSubview:commentTableView];
    [commentTableView registerNib:[UINib nibWithNibName:@"DetailCommentWithOtherTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"DetailCommentWithOtherTableViewCellReuse"];
    [commentTableView registerNib:[UINib nibWithNibName:@"NoCommentTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"NoCommentTableViewCellReuse"];
    commentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    _commentTableView = commentTableView;
    
}

#pragma mark - 点赞
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (_postDetailViewModel.homeChannelDetailModel.content.likeUsers.count > 16) {
        return 16;
    }
    return _postDetailViewModel.homeChannelDetailModel.content.likeUsers.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_postDetailViewModel.homeChannelDetailModel.content.likeUsers.count > 16) {
        if (indexPath.row == 15) {
            HomeChannelDetailMoreCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeChannelDetailMoreCollectionViewCellReuse" forIndexPath:indexPath];
            return cell;
        }
        DetailLikedHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailLikedHeaderCollectionViewCellReuse" forIndexPath:indexPath];
        cell.likeuser = _postDetailViewModel.homeChannelDetailModel.content.likeUsers[indexPath.row];
        return cell;
    }
    DetailLikedHeaderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DetailLikedHeaderCollectionViewCellReuse" forIndexPath:indexPath];
    cell.likeuser = _postDetailViewModel.homeChannelDetailModel.content.likeUsers[indexPath.row];
    return cell;
}

#pragma mark - 评论

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_postDetailViewModel.homeChannelDetailModel.content.comment.count == 0) {
        return 50;
    }
    
    
    HomeChanneldetailCommentModel *commentModel = _postDetailViewModel.homeChannelDetailModel.content.comment[indexPath.row];
    if (commentModel.replyName) {
        NSString *string = [NSString stringWithFormat:@"回复 %@: %@", commentModel.replyName, commentModel.content];
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:string width:SCREEN_WIDTH - 100 font:[UIFont systemFontOfSize:13]];
        return height + 35;
    } else {
        NSString *string = commentModel.content;
        CGFloat height = [SelfSizeTool selfSizeHeightWithString:string width:SCREEN_WIDTH - 100 font:[UIFont systemFontOfSize:13]];
        return height + 35;
    }
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (_postDetailViewModel.homeChannelDetailModel.content.comment.count == 0) {
        return 1;
    }
    
    return _postDetailViewModel.homeChannelDetailModel.content.comment.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_postDetailViewModel.homeChannelDetailModel.content.comment.count == 0) {
        NoCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoCommentTableViewCellReuse"];
        return cell;
    }
    DetailCommentWithOtherTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"DetailCommentWithOtherTableViewCellReuse"];
    cell.commentModel = _postDetailViewModel.homeChannelDetailModel.content.comment[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (_postDetailViewModel.homeChannelDetailModel.content.likeUsers.count > 16 && indexPath.row == 15) {
        NSString *pid = _postDetailViewModel.homeChannelDetailModel.content.ID;
        self.postAllPersonalPage(pid);
    } else {
        NSString *ID = _postDetailViewModel.homeChannelDetailModel.content.likeUsers[indexPath.row].ID;
        self.postPersonalPage(ID);
    }
}


- (void)setPostDetailViewModel:(PostsDetailViewModel *)postDetailViewModel {
    _postDetailViewModel = postDetailViewModel;
    
    [self setUpData];
    [self setUpFrame];
    [_likePersonCollectionView reloadData];
    [_commentTableView reloadData];
}

- (void)setUpData {
    [_headerImageButton sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qn.zheli.io/%@", _postDetailViewModel.homeChannelDetailModel.content.avatar]] forState:UIControlStateNormal];
    _nameLabel.text = _postDetailViewModel.homeChannelDetailModel.content.uname;
#warning 这里抓时间;
    _totalTime.text = [NSString convertTime: _postDetailViewModel.homeChannelDetailModel.content.voice.length];
#warning 问题;
    [_voiceImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://qn.zheli.io/%@", _postDetailViewModel.homeChannelDetailModel.content.img.firstObject.f]]];
    
    _descLabel.text = _postDetailViewModel.homeChannelDetailModel.content.desc;
    if (_postDetailViewModel.homeChannelDetailModel.content.tag.count != 0) {
        NSMutableArray *tagNameArray = [NSMutableArray array];
        for (NSInteger i = 0; i < _postDetailViewModel.homeChannelDetailModel.content.tag.count; i++) {
            [tagNameArray addObject:_postDetailViewModel.homeChannelDetailModel.content.tag[i].content];
        }
        [tagNameArray insertObject:@"#" atIndex:0];
        NSString *tagString = [tagNameArray componentsJoinedByString:@" "];
        _tagsLabel.text = tagString;
    }
}

#pragma mark - frame
- (void)setUpFrame {
    _headerImageButton.frame = _postDetailViewModel.headerImageButtonFrame;
    _vipImageView.frame = _postDetailViewModel.vipImageViewFrame;
    _nameLabel.frame = _postDetailViewModel.nameLabelFrame;
    _timeLabel.frame = _postDetailViewModel.timeLabelFrame;
    _voiceImage.frame = _postDetailViewModel.photoScrollViewFrame;
    _playButton.frame = CGRectMake(SCREEN_WIDTH - 70, CGRectGetMaxY(_voiceImage.frame) - 75, 50, 50);
    _totalTime.frame = CGRectMake(20, CGRectGetMaxY(_voiceImage.frame) - 80, 150, 60);

    _descLabel.frame = _postDetailViewModel.descLabelFrame;
    _tagsLabel.frame = _postDetailViewModel.tagsLabelFrame;
    _likePersonCollectionView.frame = _postDetailViewModel.likePersonCollectionViewFrame;
    _heaterImage.frame = _postDetailViewModel.heartImageFrame;
    _messegeImageView.frame = _postDetailViewModel.messegeImageViewFrame;
    _commentTableView.frame = _postDetailViewModel.commentDetailTableViewFrame;
    _lineView.frame = _postDetailViewModel.lineViewFrame;
    _secLineView.frame = _postDetailViewModel.secLineViewFrame;
}


- (void)playButtonAction:(UIButton *)playButton {
//    NSLog(@"播放");
    
    [self.delegate playAction:_postDetailViewModel.homeChannelDetailModel];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
